home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 February / EnigmA AMIGA RUN 04 (1996)(G.R. Edizioni)(IT)[!][issue 1996-02][Skylink CD III].iso / earcd / util1 / yk211src.lha / Yak_2.11_Src / Prefs / GadTools / Keydef_window.c < prev    next >
C/C++ Source or Header  |  1995-11-16  |  15KB  |  501 lines

  1.  
  2.  
  3. #include <exec/types.h>
  4. #include <exec/memory.h>
  5. #include <dos/dosextens.h>
  6. #include <intuition/intuition.h>
  7. #include <intuition/classes.h>
  8. #include <intuition/classusr.h>
  9. #include <intuition/imageclass.h>
  10. #include <intuition/gadgetclass.h>
  11. #include <intuition/intuitionbase.h>
  12. #include <libraries/gadtools.h>
  13. #include <libraries/commodities.h>
  14. #include <proto/commodities.h>
  15. #include <proto/exec.h>
  16. #include <proto/intuition.h>
  17. #include <proto/gadtools.h>
  18. #include <proto/wb.h>
  19. #include <string.h>
  20.  
  21. #include "code.h"
  22. #include "version.h"
  23. #include "yak.h"
  24. #include "hotkey_types.h"
  25. #include "handlers.h"
  26. #include "keydef.h"
  27. #include "gui.h"
  28. #include "KeyDef_Window.h"
  29. #include "Requesters.h"
  30. #include "yak_locale_strings.h"
  31.  
  32.  
  33. IMPORT LONG (*HandleIDCMP)(VOID);
  34.  
  35. /* Online help system */
  36. IMPORT void ShowYakHelp(char *, char *);
  37. IMPORT char *PrefsHelp;
  38.  
  39.  
  40. struct Window         *KeyDefWnd    = NULL;
  41. struct Gadget         *KeyDefGList  = NULL;
  42. struct Gadget         *KeyDefGadgets[KeyDef_CNT];
  43. UWORD                  KeyDefWidth  = 535;
  44. UWORD                  KeyDefHeight = 84;
  45. UBYTE                 *KeyDefWdt  = NULL;
  46. BOOL                   KeyDefWdi  = FALSE;
  47.  
  48. struct AppWindow      *KeyDefAppWin;      /* AppWindow pointer */
  49. IMPORT struct MsgPort *AppMsgPort;
  50.  
  51. IMPORT UBYTE     ParentWindowID;
  52. static APTR      CurObj;
  53. static YakKeyDef KeyDef;
  54.  
  55.  
  56. UBYTE *Class0Labels[5];
  57.  
  58. void
  59. InitClass0Labels(VOID)
  60. {
  61.     Class0Labels[0] = getString(RawKey_CLASS);
  62.     Class0Labels[1] = getString(RawMouse_CLASS);
  63.     Class0Labels[2] = getString(DiskInserted_CLASS);
  64.     Class0Labels[3] = getString(DiskRemoved_CLASS);
  65.     Class0Labels[4] = NULL;
  66. }
  67.  
  68.  
  69. UBYTE *Stroke0Labels[4];
  70.  
  71. void
  72. InitStroke0Labels(VOID)
  73. {
  74.     Stroke0Labels[0] = getString(Down_STROKE);
  75.     Stroke0Labels[1] = getString(Up_STROKE);
  76.     Stroke0Labels[2] = getString(Down_Up_STROKE);
  77.     Stroke0Labels[3] = NULL;
  78. }
  79.  
  80.  
  81. UWORD KeyDefGTypes[] = 
  82. {
  83.         CYCLE_KIND,
  84.         CYCLE_KIND,
  85.         STRING_KIND,
  86.         BUTTON_KIND,
  87.         BUTTON_KIND,
  88.         BUTTON_KIND,
  89.         BUTTON_KIND
  90. };
  91.  
  92. struct NewGadget KeyDefNGad[] = {
  93.          66, 13, 166, 13, NULL, NULL, GD_Class,  PLACETEXT_LEFT, NULL, CLASS_STRING,
  94.         343, 13, 186, 13, NULL, NULL, GD_Stroke, PLACETEXT_LEFT, NULL, STROKE_STRING,
  95.         108, 38, 421, 14, NULL, NULL, GD_Desc,   PLACETEXT_LEFT, NULL, DESC_STRING,
  96.           6, 68, 100, 13, NULL, NULL, GD_Ok,     PLACETEXT_IN,   NULL, OK_STRING,
  97.         429, 68, 100, 13, NULL, NULL, GD_Cancel, PLACETEXT_IN,   NULL, CANCEL_STRING,
  98.         147, 68, 100, 13, NULL, NULL, GD_Clear,  PLACETEXT_IN,   NULL, CLEAR_STRING,
  99.         288, 68, 100, 13, NULL, NULL, GD_Help,   PLACETEXT_IN,   NULL, HELP_STRING
  100. };
  101.  
  102.  
  103. ULONG KeyDefGTags[] = {
  104.         (GTCY_Labels), (ULONG)&Class0Labels[ 0 ], (TAG_DONE),
  105.         (GTCY_Labels), (ULONG)&Stroke0Labels[ 0 ], (TAG_DONE),
  106.         (GTST_MaxChars), 256, (TAG_DONE),
  107.         (TAG_DONE),
  108.         (TAG_DONE),
  109.         (TAG_DONE),
  110.         (TAG_DONE)
  111. };
  112.  
  113.  
  114.  
  115. /* initialise all gadgets of keydef window */
  116. static void
  117. InitKeyDefGadgets(VOID)
  118. {
  119.     UBYTE SeqString[127];
  120.     BOOL SeqOk = BuildKeyDef(&KeyDef, SeqString);
  121.  
  122.     InitWindowGadget(GDX_Class, GTCY_Active, KeyDef.ykd_Class, KEYDEF_WINDOW);
  123.     if (ParentWindowID == MCYCLING_WINDOW)
  124.     {
  125.         InitWindowGadget(GDX_Class, GA_Disabled, KeyDef.ykd_Class, KEYDEF_WINDOW);
  126.     }
  127.     InitWindowGadget(GDX_Desc,   GTST_String, (LONG)SeqString,   KEYDEF_WINDOW);
  128.     InitWindowGadget(GDX_Stroke, GTCY_Active, KeyDef.ykd_Stroke, KEYDEF_WINDOW);
  129.     InitWindowGadget(GDX_Stroke, GA_Disabled, KeyDef.ykd_Class,  KEYDEF_WINDOW);
  130.     InitWindowGadget(GDX_Ok,     GA_Disabled, !SeqOk,            KEYDEF_WINDOW);
  131. }
  132.  
  133.  
  134.  
  135. /* switch from misc window to keydef window */
  136. BOOL
  137. SwitchToKeyDefWindow(UBYTE I_ParentWindowID, APTR I_object, char *I_action)
  138. {
  139.     static char title[80];
  140.     char       *def;
  141.  
  142.     strcpy(title, getString(KeyDef_Window_TITLE));
  143.     strcat(title, " ");
  144.     if (I_action)
  145.     {
  146.         strcat(title, I_action); 
  147.     }
  148.     else
  149.     {
  150.         strcat(title, "<<unnamed>>"); 
  151.     }
  152.     KeyDefWdt = title;
  153.  
  154.     ParentWindowID = I_ParentWindowID;
  155.     CurObj         = I_object;
  156.  
  157.     if (ParentWindowID == HOTKEY_WINDOW)
  158.         def = ((YakHotKey *)CurObj)->yhk_KeyDef;
  159.     else
  160.         def = ((YakHandler *)CurObj)->KeyDef;
  161.  
  162.         ParseKeyDef(def, &KeyDef);
  163.  
  164.     CloseWindowID(ParentWindowID);
  165.  
  166.     return ShowKeyDefWindow();
  167. }
  168.  
  169.  
  170. /* handle KeyDef window events */
  171. LONG
  172. HandleKeyDefIDCMP(VOID)
  173. {
  174.     struct IntuiMessage *msg;
  175.     struct Gadget       *gadget;
  176.     ULONG                class;
  177.     UWORD                code;
  178.     UWORD                qual, newqual;
  179.     BOOL                 close=FALSE;
  180.     LONG                 ret=RET_OKAY;
  181.  
  182.     while (!close && (msg = GT_GetIMsg(KeyDefWnd->UserPort)))
  183.     {
  184.         class  = msg->Class;
  185.         code   = msg->Code;
  186.         qual   = msg->Qualifier;
  187.         gadget = (struct Gadget *)msg->IAddress;
  188.         GT_ReplyIMsg(msg);
  189.  
  190.         switch (class)
  191.         {
  192.           case GADGETUP:
  193.           case GADGETDOWN:
  194.             switch (gadget->GadgetID)
  195.             {
  196.               case GD_Class:
  197.                 KeyDef.ykd_Class  = code;
  198.                 KeyDef.ykd_Stroke = HKSTROKE_DOWN;
  199.                 KeyDef.ykd_Code   = NO_IECODE;
  200.                 KeyDef.ykd_Qual = KeyDef.ykd_IQual = KeyDef.ykd_SQual = NO_IEQUALIFIER;
  201.  
  202.                 InitKeyDefGadgets();
  203.                 break;
  204.  
  205.               case GD_Stroke:
  206.                 if ((KeyDef.ykd_Stroke=code) == HKSTROKE_UP)
  207.                 {
  208.                     KeyDef.ykd_Qual  &= ~IEQUALIFIER_REPEAT;
  209.                     KeyDef.ykd_IQual &= ~IEQUALIFIER_REPEAT;
  210.                 }
  211.  
  212.                 InitKeyDefGadgets();
  213.                 break;
  214.  
  215.               case GD_Desc:
  216.                 if (ParseKeyDef(GetString(gadget), &KeyDef) == -1)
  217.                     PostError(getString(Invalid_hotkey_specif_ERR));
  218.  
  219.                 InitKeyDefGadgets();
  220.  
  221.                 break;
  222.  
  223.               case GD_Cancel:
  224.                 CloseKeyDefWindow();
  225.                 if (!ShowWindowID(ParentWindowID))
  226.                     ret = RET_QUIT;
  227.                 close = TRUE;
  228.                 break;
  229.  
  230.               case GD_Clear:
  231.                 KeyDef.ykd_Stroke = HKSTROKE_DOWN;
  232.                 KeyDef.ykd_Code   = NO_IECODE;
  233.                 KeyDef.ykd_Qual   = 
  234.                 KeyDef.ykd_IQual  = 
  235.                 KeyDef.ykd_SQual  = NO_IEQUALIFIER;
  236.                 InitKeyDefGadgets();
  237.                 break;
  238.  
  239.               case GD_Help:
  240.                 ShowYakHelp(PrefsHelp, "Hotkey Description String");
  241.                 break;
  242.  
  243.               case GD_Ok:
  244.                 if (ParentWindowID == HOTKEY_WINDOW)
  245.                 {
  246.                     if (ModifyYHKKeyDef(CurObj, GetString(KeyDefGadgets[GDX_Desc])) == TRUE)
  247.                     {
  248.  
  249.                         /* Turn hotkey on active by default */
  250.                         ((YakHotKey *)CurObj)->yhk_State = YKHK_ACTIVE;
  251.                     }
  252.  
  253.                 }
  254.                 else
  255.                 {
  256.                     char *newdef;
  257.  
  258.                     if (newdef=DupStr(GetString(KeyDefGadgets[GDX_Desc])))
  259.                     {
  260.                         YakHandler *curhd=(YakHandler *)CurObj;
  261.                         if (curhd->KeyDef) FreeVec(curhd->KeyDef);
  262.                         curhd->KeyDef = newdef;
  263.                     }
  264.                     else
  265.                        PostError(getString(Allocation_ERR));
  266.                 }
  267.                 CloseKeyDefWindow();
  268.                 if (!ShowWindowID(ParentWindowID))
  269.                     ret = RET_QUIT;
  270.                 close = TRUE;
  271.                 break;
  272.  
  273.             }                   /* switch (gadget->GadgetID) */
  274.             break;
  275.  
  276.           case IDCMP_RAWKEY:
  277.             if ( (!(code & IECODE_UP_PREFIX) || code == 0xe2 ) && /* a downstroke || capslock up*/
  278.                 ((KeyDef.ykd_Class == HKCLASS_RAWKEY) || (KeyDef.ykd_Class == HKCLASS_RAWMOUSE)) )
  279.             {
  280.                 if (newqual = IsQualifierPad(code))
  281.                 {
  282.                     newqual &= ~KeyDef.ykd_SQual;
  283.                     newqual &= ~KeyDef.ykd_IQual;
  284.  
  285.                     /* Toggle Qualifier */
  286.                     KeyDef.ykd_Qual ^= newqual;
  287.                 }
  288.                 else
  289.                 {
  290.                     if (KeyDef.ykd_Class == HKCLASS_RAWKEY)
  291.                     {
  292.                         if ((code == KeyDef.ykd_Code) && (KeyDef.ykd_Stroke != HKSTROKE_UP))
  293.                         {
  294.                             /* Toggle Repeat Keyword */
  295.                             if(!(KeyDef.ykd_IQual & IEQUALIFIER_REPEAT))
  296.                                 KeyDef.ykd_Qual  ^= IEQUALIFIER_REPEAT;
  297.  
  298.                         }
  299.                         else    /* New key or upstroke mode */
  300.                         {
  301.                             KeyDef.ykd_Qual  &= ~IEQUALIFIER_KEYS;
  302.                             KeyDef.ykd_IQual &= ~IEQUALIFIER_KEYS;
  303.                             KeyDef.ykd_Qual  |= qual & IEQUALIFIER_NUMERICPAD;
  304.                             KeyDef.ykd_Code   = code;
  305.                         }
  306.                     }
  307.                 }
  308.  
  309.                 InitKeyDefGadgets();
  310.             }
  311.             break;
  312.  
  313.           case IDCMP_MOUSEBUTTONS:
  314.             if (!(code & IECODE_UP_PREFIX) && /* a downstroke */
  315.                 ((KeyDef.ykd_Class == HKCLASS_RAWKEY) || (KeyDef.ykd_Class == HKCLASS_RAWMOUSE)) )
  316.             {
  317.                 switch(code)
  318.                 {
  319.                   case IECODE_RBUTTON:
  320.                     newqual = IEQUALIFIER_RBUTTON;
  321.                     break;
  322.                   case IECODE_LBUTTON:
  323.                     newqual = IEQUALIFIER_LEFTBUTTON;
  324.                     break;
  325.                   case IECODE_MBUTTON:
  326.                     newqual = IEQUALIFIER_MIDBUTTON;
  327.                     break;
  328.                   default:
  329.                     newqual = NO_IEQUALIFIER;
  330.                     break;
  331.                 }
  332.  
  333.                 newqual &= ~KeyDef.ykd_IQual;
  334.  
  335.                 if (KeyDef.ykd_Class == HKCLASS_RAWMOUSE)
  336.                 {
  337.                     if (KeyDef.ykd_Code == code)
  338.                     {
  339.                         KeyDef.ykd_Qual &= ~newqual;
  340.                         KeyDef.ykd_Code  = NO_IECODE;
  341.                     }
  342.                     else
  343.                     {
  344.                         KeyDef.ykd_Qual |= newqual;
  345.                         KeyDef.ykd_Code  = code;
  346.                     }
  347.                 }
  348.                 else
  349.                     KeyDef.ykd_Qual  ^= newqual;
  350.  
  351.                 InitKeyDefGadgets();
  352.             }
  353.             break;
  354.  
  355.           case REFRESHWINDOW:
  356.             GT_BeginRefresh(KeyDefWnd);
  357.             GT_EndRefresh(KeyDefWnd, TRUE);
  358.             break;
  359.  
  360.         }                       /* switch (class) */
  361.  
  362.     }                           /* while more messages */
  363.  
  364.     return ret;
  365. }
  366.  
  367.  
  368. void 
  369. CloseKeyDefWindow( void )
  370. {
  371.         CloseOneWindow (KeyDefWnd, NULL, KeyDefGList, KeyDefAppWin);
  372.         KeyDefWnd = NULL;
  373.         
  374.         /* Re-enable Yak now that our job is finished */
  375.         BrokerCommand("Yak", CXCMD_ENABLE);
  376.  
  377. }
  378.  
  379.  
  380. void 
  381. KeyDefRender( void )
  382. {
  383.     UWORD Width, Height, X, Y;
  384.  
  385.     ComputeFont( KeyDefWidth, KeyDefHeight );
  386.  
  387.     Width = ComputeX( KeyDefWidth );
  388.     Height= ComputeY( 65);
  389.  
  390.     X = OffX;
  391.     Y = OffY;
  392.     DrawThinBevelBox( Scr, KeyDefWnd->RPort, X,Y,
  393.                      Width,
  394.                      Height,
  395.                      FALSE );
  396.  
  397.     Y = OffY + ComputeY(65);
  398.     DrawThinBevelBox( Scr, KeyDefWnd->RPort, X,Y,
  399.                      Width,
  400.                      ComputeY(KeyDefHeight)-Height,
  401.                      FALSE );
  402. }
  403.  
  404.  
  405. int 
  406. OpenKeyDefWindow(VOID)
  407. {
  408.         struct NewGadget        ng;
  409.         struct Gadget   *g;
  410.         WORD            wleft=WindowLeft, wtop=WindowTop;
  411.         UWORD           ww, wh;
  412.         UBYTE           lc, tc;
  413.  
  414.         ComputeFont( KeyDefWidth, KeyDefHeight );
  415.  
  416.         ww = OffX + ComputeX( KeyDefWidth ) + Scr->WBorRight;
  417.         wh = OffY + ComputeY( KeyDefHeight ) + Scr->WBorBottom;
  418.  
  419.         SetUpWindowCoordinates(ww, wh, &wleft, &wtop);
  420.  
  421.         if ( ! ( g = CreateContext( &KeyDefGList )))
  422.                 return( 1L );
  423.  
  424.         for( lc = 0, tc = 0; lc < KeyDef_CNT; lc++ ) {
  425.  
  426.                 CopyMem((char * )&KeyDefNGad[ lc ], (char * )&ng, (long)sizeof( struct NewGadget ));
  427.  
  428.                 ng.ng_VisualInfo = VisualInfo;
  429.                 ng.ng_TextAttr   = Font;
  430.                 ng.ng_LeftEdge   = OffX + ComputeX( ng.ng_LeftEdge );
  431.                 ng.ng_TopEdge    = OffY + ComputeY( ng.ng_TopEdge );
  432.                 ng.ng_Width      = ComputeX( ng.ng_Width );
  433.                 ng.ng_Height     = ComputeY( ng.ng_Height);
  434.  
  435.                 KeyDefGadgets[ lc ] = g = CreateGadgetA((ULONG)KeyDefGTypes[ lc ], g, &ng, ( struct TagItem * )&KeyDefGTags[ tc ] );
  436.  
  437.                 while( KeyDefGTags[ tc ] ) tc += 2;
  438.                 tc++;
  439.  
  440.                 if ( NOT g )
  441.                         return( 2L );
  442.         }
  443.  
  444.         if ( ! ( KeyDefWnd = OpenWindowTags( NULL,
  445.                                 WA_Left,         wleft,
  446.                                 WA_Top,          wtop,
  447.                                 WA_Width,        ww,
  448.                                 WA_Height,       wh,
  449.                                 WA_IDCMP,        CYCLEIDCMP|TEXTIDCMP|BUTTONIDCMP|IDCMP_MOUSEBUTTONS|IDCMP_RAWKEY|IDCMP_REFRESHWINDOW,
  450.                                 WA_Flags,        WFLG_ACTIVATE|WFLG_DRAGBAR|WFLG_DEPTHGADGET|WFLG_SMART_REFRESH|WFLG_RMBTRAP,
  451.                                 WA_Gadgets,      KeyDefGList,
  452.                                 WA_Title,        KeyDefWdt,
  453.                                 WA_ScreenTitle,  getString(COPYRIGHT_STRING),
  454.                                 WA_PubScreenName,PubScreenName,
  455.                                 TAG_DONE )))
  456.         return( 4L );
  457.  
  458.         KeyDefAppWin = AddAppWindowA(0, 0, KeyDefWnd, AppMsgPort, NULL);
  459.  
  460.         GT_RefreshWindow( KeyDefWnd, NULL );
  461.  
  462.         KeyDefRender();
  463.  
  464.         return( 0L );
  465. }
  466.  
  467.  
  468. /* show our KeyDef window */
  469. BOOL
  470. ShowKeyDefWindow(VOID)
  471. {
  472.     if (!KeyDefWdi)
  473.     {
  474.         KeyDefWdi = TRUE;
  475.  
  476.         LocalizeNGadLabels (KeyDef_CNT, KeyDefNGad);
  477.         InitClass0Labels ();
  478.         InitStroke0Labels ();
  479.     }
  480.  
  481.     if (!OpenKeyDefWindow())  /* like the name says... */
  482.     {
  483.         /* Disable Yak so that it doesn't interfere with us */
  484.         BrokerCommand("Yak", CXCMD_DISABLE);
  485.  
  486.         InitKeyDefGadgets();
  487.         wndsigflag = 1 << KeyDefWnd->UserPort->mp_SigBit;
  488.         curwin = KeyDefWnd;
  489.         curwinID = KEYDEF_WINDOW;
  490.         HandleIDCMP = HandleKeyDefIDCMP;
  491.         return TRUE;
  492.     }
  493.  
  494.     FreeGadgets(KeyDefGList);
  495.     CloseDownScreen();
  496.     curwin =  NULL;
  497.     wndsigflag = 0L;
  498.     return FALSE;
  499. }
  500.  
  501.